package com.shadow.bus.main.search;

import com.alibaba.fastjson.TypeReference;
import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.SearchResultEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyu
 * @date 2018/7/5
 */

public class SearchDataConverter {
    public List<MultipleItemEntity> convert(final String response, ArrayList<String> keys) {
        List<MultipleItemEntity> list = new ArrayList<>();
        SearchResultEntity searchResultEntity = new ObjConverter<SearchResultEntity>().
                convert(response, new TypeReference<SearchResultEntity>() {
                });
        ArrayList<PartEntity> listAccessory = searchResultEntity.getListAccessory();
        ArrayList<PartEntity> listComponent = searchResultEntity.getListComponent();
        if (listAccessory != null) {
            for (PartEntity partEntity : listAccessory) {
                list.add(MultipleItemEntity.builder()
                        .setItemType(ItemType.TYPE_PART_ACCESSORY)
                        .setField(ItemFiled.ITEM_OBJ, partEntity)
                        .build());
            }
        }
        if (listComponent != null) {
            for (PartEntity partEntity : listComponent) {
                list.add(MultipleItemEntity.builder()
                        .setItemType(ItemType.TYPE_PART_COMPONENT)
                        .setField(ItemFiled.ITEM_OBJ, partEntity)
                        .build());
            }
        }
        ArrayList<String> searchKeys = searchResultEntity.getListKey();
        if (searchKeys != null) {
            keys.addAll(searchKeys);
        }
        return list;
    }
}
